
/** @module token */

import Cookies from 'js-cookie'
const config = require('../config')

/**setToken的参数*/
export interface SetTokenParams{
	token:string,
	remember:boolean,
}

export interface SetToken{
	(params:SetTokenParams):void;
}

export interface GetToken{
	():string|undefined;
}

export interface RemoveToken{
	():void;
}

/**
 * 设置Token
 * @param {setTokenParams} params 
 * @param {string} params.token
 * @param {boolean} params.remember
 */
export let setToken:SetToken=function (params){
	if(params.remember){
		Cookies.set('token', params.token, { expires: config.tokenExpires })
	}else{
		sessionStorage.setItem('token',params.token)
	}
}

/**
 * 获取Token
 * @return {string|undefined} 
 */
export let getToken:GetToken=function (){
	var sessionToken=sessionStorage.getItem('token')
	var cookieToken=Cookies.get('token')
	return sessionToken||cookieToken
}
/** 移除Token */
export let removeToken:RemoveToken=function (){
	Cookies.remove('token')
	sessionStorage.removeItem('token')
}
